﻿Imports System.Net.Sockets

Partial Public Class Manager

    Public Shared Event NodeStatusChanged(ByVal node As Node)
    Public Shared Event NodeStatusChangedGUI(ByVal node As Node)
    Friend Shared Sub OnNodeStatusChanged(ByVal node As Node)
        RaiseEvent NodeStatusChanged(node)
        Framework.Threading.Invoke(AddressOf OnNodeStatusChanged_CallBack, node)
    End Sub
    Private Shared Sub OnNodeStatusChanged_CallBack(ByVal node As Node)
        RaiseEvent NodeStatusChangedGUI(node)
    End Sub

    Public Class NetworkDataPackage
        Public ReadOnly Node As Framework.Net.Node
        Public ReadOnly Len As Integer
        Public ReadOnly Code As String
        Public ReadOnly RawData() As Byte
        Public ReadOnly RawDataRealSize As Integer
        Public ReadOnly TextData As String
        Public ReadOnly Direction As PackageDirection

        Friend Sub New(ByVal node As Framework.Net.Node, ByVal len As Integer, ByVal code As String, ByVal rawData As Byte(), ByVal direction As PackageDirection)
            Me.Node = node
            Me.Len = len
            Me.Direction = direction
            Me.RawData = rawData
            Me.RawDataRealSize = rawDataRealSize
            Me.Code = code
        End Sub
        Friend Sub New(ByVal node As Framework.Net.Node, ByVal len As Integer, ByVal code As String, ByVal textData As String, ByVal direction As PackageDirection)
            Me.node = node
            Me.Len = len
            Me.Direction = direction
            Me.TextData = textData
            Me.Code = code
        End Sub
        Public Enum PackageDirection
            Outgoing
            Incoming
            Information
        End Enum
    End Class
    Public Shared Event NetworkTraceInfo(ByVal e As NetworkDataPackage)
    Friend Shared Sub OnNetworkTraceInfo(ByVal node As Framework.Net.Node, ByVal code As String, ByVal rawData As Byte(), ByVal rawDataRealSize As Integer, ByVal direction As NetworkDataPackage.PackageDirection)
        If Not Settings.TraceTraffic Then Return
        Dim e As New NetworkDataPackage(node, rawDataRealSize, code, rawData, direction)
        OnNetworkTraceInfo(e)
    End Sub
    Friend Shared Sub OnNetworkTraceInfo(ByVal node As Framework.Net.Node, ByVal code As String, ByVal dataInfo As String, ByVal dataLen As Integer, ByVal direction As NetworkDataPackage.PackageDirection)
        If Not Settings.TraceTraffic Then Return
        Dim e As New NetworkDataPackage(node, dataLen, code, dataInfo, direction)
        OnNetworkTraceInfo(e)
    End Sub
    Friend Shared Sub OnNetworkTraceInfo(ByVal node As Framework.Net.Node, ByVal code As String, ByVal rawData As Byte(), ByVal direction As NetworkDataPackage.PackageDirection)
        If Not Settings.TraceTraffic Then Return
        Dim iLen As Integer = 0
        If rawData IsNot Nothing Then iLen = rawData.Length
        Dim e As New NetworkDataPackage(node, iLen, code, rawData, direction)
        OnNetworkTraceInfo(e)
    End Sub
    Friend Shared Sub OnNetworkTraceInfo(ByVal node As Framework.Net.Node, ByVal information As String)
        If Not Settings.TraceTraffic Then Return
        Dim e As New NetworkDataPackage(node, -1, String.Empty, information, NetworkDataPackage.PackageDirection.Information)
        OnNetworkTraceInfo(e)
    End Sub
    Friend Shared Sub OnNetworkTraceInfo(ByVal e As NetworkDataPackage)
        If Not Settings.TraceTraffic Then Return
        Framework.Threading.Invoke(AddressOf OnNetworkTraceInfo_Callback, e)
    End Sub
    Private Shared Sub OnNetworkTraceInfo_Callback(ByVal e As NetworkDataPackage)
        RaiseEvent NetworkTraceInfo(e)
    End Sub

    Public Shared Event SettingsChanged()
    Friend Shared Sub OnSettingsChanged()
        Framework.Threading.Invoke(AddressOf OnSettingsChanged_Callback)
    End Sub
    Private Shared Sub OnSettingsChanged_Callback()
        RaiseEvent SettingsChanged()
    End Sub
End Class
